package com.syz.mall.controller;

import com.sun.org.apache.bcel.internal.generic.NEW;
import com.syz.mall.mapper.UserAddressMapper;
import com.syz.mall.mapper.UserMapper;
import com.syz.mall.pojo.User;
import com.syz.mall.pojo.UserAddress;
import com.syz.mall.pojo.UserOtherData;
import com.syz.mall.service.IUserService;
import com.syz.mall.util.MD5Util;
import com.syz.mall.util.Result;
import org.apache.ibatis.javassist.runtime.Inner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;



    @RequestMapping("/checkUserLogin")
    @ResponseBody
    public Result checkUserLogin(HttpSession session) {
        System.out.println("UserController.checkUserLogin");
        User user = (User) session.getAttribute("user");
        return user != null ? Result.ok() : Result.error();
    }

    @RequestMapping("/login")
    @ResponseBody
    public Result login(String username, String password, HttpSession session) {

        String md5password=MD5Util.MD5Encode(password);
        User user = userService.login(username, md5password);
        if (user != null) {
            UserOtherData userOtherData=userService.selectOtherDataByUserId(user.getId());
            session.setAttribute("nickName",userOtherData.getNickname());
            session.setAttribute("user", user);
            session.setMaxInactiveInterval(0);
            return Result.ok("登录成功");
        } else {
            return Result.error("登录失败");
        }
    }

    @RequestMapping("/getLoginPage")
    public String getLoginPage() {
        return "login";
    }

    @RequestMapping("/getUserAddressPage")
    public String getUAP(Model model,HttpSession session){
        User user= (User) session.getAttribute("user");
        List<UserAddress> list=userService.selectAllAddressByUserId(user.getId());
        model.addAttribute("list",list);
        return "userCenter/user_address";
    }
    @RequestMapping("/getUserDataPage")
    public String getUDP(Model model,HttpSession session){
        User user= (User) session.getAttribute("user");
        UserOtherData userOtherData=userService.selectOtherDataByUserId(user.getId());
        model.addAttribute("user",user);
        model.addAttribute("otherData",userOtherData);
        return "userCenter/user_data";
    }
    @RequestMapping("/getUserPwdModifyPage")
    public String getUPMP(){
        return "userCenter/user_pwd_modify";
    }
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("user");
        return "login";
    }
    @RequestMapping("/register")
    @ResponseBody
    public Result register(String username,String password){
        String md5password= MD5Util.MD5Encode(password);
        if(userService.register(username,md5password)){
            return Result.ok("注册成功");
        }else{
            return Result.error("注册失败");
        }
    }
    @RequestMapping("/addAddress")
    @ResponseBody
    public Result addAddress(UserAddress userAddress,HttpSession session){
        User user = (User) session.getAttribute("user");
        userAddress.setUserId(user.getId());
        if(userService.addAddress(userAddress)){
            return Result.ok("添加成功");
        }else{
            return Result.error("添加失败");
        }
    }
    @RequestMapping("/queryAddress")
    @ResponseBody
    public List<UserAddress> queryAddress(HttpSession session){
        User user = (User) session.getAttribute("user");
        return userService.selectAllAddressByUserId(user.getId());
    }
    @RequestMapping("/deleteAddress")
    @ResponseBody
    public Result deleteAddress(Integer id){
        if(userService.deleteAddressById(id)){
            return Result.ok("删除成功");
        }else{
            return Result.error("删除失败");
        }
    }
    @RequestMapping("/updateAddress")
    @ResponseBody
    public Result updateAddress(UserAddress userAddress){
        if(userService.updateAddress(userAddress)){
            return Result.ok("更新成功");
        }else{
            return Result.error("更新失败");
        }
    }
    @RequestMapping("/checkPassword")
    @ResponseBody
    public Result checkPassword(HttpSession session,String oldPassword){
        User user= (User) session.getAttribute("user");
        String md5oldPassword=MD5Util.MD5Encode(oldPassword);
        if(userService.checkPassword(user.getId(),md5oldPassword)){
            return Result.ok("原密码正确");
        }else {
            return Result.error("原密码错误");
        }
    }
    @RequestMapping("/changePassword")
    @ResponseBody
    public Result changePassword(HttpSession session,String newPassword){
        User user= (User) session.getAttribute("user");
        String md5oldPassword=MD5Util.MD5Encode(newPassword);
        if(userService.changePassword(user.getId(),md5oldPassword)){
            return Result.ok("修改成功");
        }else {
            return Result.error("修改失败");
        }
    }

    @RequestMapping("/updateOtherDataSelective")
    @ResponseBody
    public Result updateOtherDataSelective(UserOtherData userOtherData,HttpSession session){
        User user= (User) session.getAttribute("user");
        userOtherData.setUserId(user.getId());
        if(userService.updateOtherDataSelective(userOtherData)){
            return Result.ok("修改成功");
        }else {
            return Result.error("修改失败");
        }
    }
    @RequestMapping("/queryQuestion")
    @ResponseBody
    public Result queryQuestion(String username){
        Integer id=userService.queryIdByUsername(username);
        if(id==null){
            return Result.error("无此用户");
        }else{
            UserOtherData userOtherData=userService.selectOtherDataByUserId(id);
            String question=userOtherData.getQuestion();
            if(question==null || question.equals("")){
                return Result.error("此用户暂无设置密保问题");
            }else {
                return Result.ok(question);
            }
        }
    }
    @RequestMapping("/resetPwdByQuestion")
    @ResponseBody
    public Result resetPwdByQuestion(String username,String answer,String newPassword){
        Integer id=userService.queryIdByUsername(username);
        UserOtherData userOtherData=userService.selectOtherDataByAnswer(id,answer);
        if(userOtherData==null){
            return Result.error("答案不正确");
        }else{
            String md5password=MD5Util.MD5Encode(newPassword);
            if(userService.changePassword(id,md5password)){
                return Result.ok("更改成功");
            }else{
                return Result.error("更改失败");
            }
        }
    }
    @RequestMapping("/collectProduct")
    @ResponseBody
    public Result collectProduct(Integer productId,HttpSession session){
        User user= (User) session.getAttribute("user");
        if(userService.collectProduct(user.getId(),productId)){
            return Result.ok("收藏成功");
        }else {
            return Result.error("收藏失败");
        }
    }
    @RequestMapping("/checkIfCollect")
    @ResponseBody
    public Result checkIfCollect(Integer productId,HttpSession session){
        User user= (User) session.getAttribute("user");
        if(userService.checkIfCollect(user.getId(),productId)){
            return Result.ok("已收藏");
        }else{
            return Result.error("未收藏");
        }
    }

    @RequestMapping("/cancelCollect")
    @ResponseBody
    public Result cancelCollect(Integer productId,HttpSession session){
        User user= (User) session.getAttribute("user");
        if(userService.cancelCollect(user.getId(),productId)){
            return Result.ok("取消收藏成功");
        }else{
            return Result.error("取消失败");
        }
    }

}
